#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("桥洞音乐");
    this->setWindowIcon(QIcon(":/img/aaa.png"));

    ui->listWidget->setIconSize(QSize(40, 40));
    QFont qf = QFont();
    qf.setPointSize(16);
    ui->listWidget->setFont(qf);

    play = new QMediaPlayer;
    play->setVolume(30);

    //    ui->listWidget->setCurrentRow(ui->listWidget->)
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    playMusic();
}

void MainWindow::on_pushButton_clicked()
{
    ui->listWidget->takeItem(ui->listWidget->currentRow());
}

void MainWindow::searchMusic()
{
    qDebug() << "开始搜索";
    QString key = ui->searchMusiclineEdit->text();
    bool isSearchEmpty = key.isEmpty();

    // 先重置所有项为显示状态
    for (int i = 0; i < ui->listWidget->count(); ++i)
    {
        ui->listWidget->item(i)->setHidden(false);
    }

    if (isSearchEmpty)
    {
        // 搜索内容为空时，显示所有项，不选中任何项
        ui->listWidget->setCurrentRow(-1);
        return;
    }

    auto matchedItems = ui->listWidget->findItems(key, Qt::MatchStartsWith);
    // 若有匹配项，选中第一个
    if (!matchedItems.isEmpty())
    {
        int row = ui->listWidget->row(matchedItems.first()); // 获取第一个匹配项的行号
        ui->listWidget->setCurrentRow(row);                  // 选中该行
        for (int i = 0; i < ui->listWidget->count(); i++)
        {
            ui->listWidget->item(i)->setHidden(true);
        }
        for (auto x : matchedItems)
        {
            x->setHidden(false);
        }
    }
    else
    {
        // 无匹配项时的处理
        ui->listWidget->setCurrentRow(-1);
    }
}

void MainWindow::on_Search_pbtn_clicked()
{
    searchMusic();
}

void MainWindow::on_searchMusiclineEdit_returnPressed()
{
    searchMusic();
}

void MainWindow::on_prevPbtn_clicked()
{
    int count = ui->listWidget->count();
    if (count == 0)
    {
        return;
    }

    int index = ui->listWidget->currentRow();
    if (index == -1)
    {
        return;
    }
    int startIndex = index;

    do
    {
        if (index > 0)
        {
            index--;
        }
        else
        {
            index = count - 1;
        }
        if (!ui->listWidget->item(index)->isHidden())
        {
            ui->listWidget->setCurrentRow(index);
            return;
        }

    } while (index != startIndex);
    ui->listWidget->setCurrentRow(index);
}

void MainWindow::on_nextPbtn_clicked()
{
    int count = ui->listWidget->count();
    if (count == 0 || count == 1)
    {
        return;
    }

    int index = ui->listWidget->currentRow();
    if (index == -1)
    {
        return;
    }
    int startIndex = index;

    do
    {
        if (index < count - 1)
        {
            index++;
        }
        else
        {
            index = 0;
        }
        if (!ui->listWidget->item(index)->isHidden())
        {
            ui->listWidget->setCurrentRow(index);
            return;
        }

    } while (index != startIndex);
    ui->listWidget->setCurrentRow(index);
}

void MainWindow::on_nextPbtn_2_clicked()
{
    dirPath = QFileDialog::getExistingDirectory(this, "获取文件目录", "C:/Users/LzMian/Desktop/");
    QStringList list = QDir(dirPath).entryList();
    for (auto a : list)
    {
        if (a == "." || a == "..")
            continue;

        QStringList parts = a.split(".");
        if (parts.size() < 2 || parts.last().isEmpty())
            continue;
        QString fileName = parts.first();
        QString suffix = parts.last();
        if (suffix.compare("mp3", Qt::CaseInsensitive) != 0)
            continue;
        qDebug() << dirPath +"/" + fileName + ".png";
        ui->listWidget->addItem(new QListWidgetItem(QIcon(dirPath +"/" + fileName + ".png"), fileName));
    }
}

void MainWindow::on_nextPbtn_3_clicked()
{
    playMusic();
}

void MainWindow::playMusic(){
    QListWidgetItem* currentItem = ui->listWidget->currentItem();
    if (!currentItem) {
        return;
    }

    QString fileName = currentItem->text();

    QDir dir(dirPath);
    QString filePath = dir.filePath(fileName + ".mp3");

    if (!QFile::exists(filePath)) {
        qWarning() << "文件不存在：" << filePath;
        return;
    }

    play->setMedia(QUrl::fromLocalFile(filePath));
    play->play();
}
